package com.gzs.zhdj.controller;

import com.gzs.zhdj.domain.entity.PartyMember;
import com.gzs.zhdj.domain.dto.PartyMemberDto;
import com.gzs.zhdj.domain.vo.PartyMemberLoginVo;
import com.gzs.zhdj.properties.JwtProperties;
import com.gzs.zhdj.service.PartyMemberService;
import com.gzs.zhdj.service.AdminService;
import com.gzs.zhdj.utils.CommonResponse;
import com.gzs.zhdj.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gzs.zhdj.constant.JwtClaimsConstant;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/zhdj")
public class LoginController {
    @Autowired
    AdminService adminService;
    @Autowired
    JwtProperties jwtProperties;
    @Autowired
    PartyMemberService partyMemberService;
    @PostMapping("/login")
    public CommonResponse adminLogin(@RequestBody PartyMemberDto partyMemberDto) {
        log.info("管理员登录: {}", partyMemberDto);
        PartyMember p = adminService.adminLogin(partyMemberDto);
        //登录成功,生成令牌,下发令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.PartyMember_ID, p.getId());
        String token = JwtUtils.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        PartyMemberLoginVo partyMemberLoginVo = PartyMemberLoginVo.builder()
                .id(p.getId())
                .name(p.getName())
                .token(token)
                .build();

        return CommonResponse.ok(partyMemberLoginVo);
    }
    @PostMapping("/partymemberlogin")
    public CommonResponse partyMemberLogin(@RequestBody PartyMemberDto partyMemberDto) {
        log.info("党员登录: {}", partyMemberDto);
        PartyMember p = partyMemberService.partyMemberLogin(partyMemberDto);
        //登录成功,生成令牌,下发令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.PartyMember_ID, p.getId());
        String token = JwtUtils.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        PartyMemberLoginVo partyMemberLoginVo = PartyMemberLoginVo.builder()
                .id(p.getId())
                .name(p.getName())
                .token(token)
                .build();

        return CommonResponse.ok(partyMemberLoginVo);
    }
}
